local PartnersData = class("CoreProp")

function PartnersData:ctor(  )
	-- body
	self._isPartnersInit = false
	self._fightTeam = {}
end

function PartnersData:_initPartners()
	if not self._isPartnersInit then
		local PartnerHelper = require("app.partner.PartnerHelper")
		self._partners = PartnerHelper.initPartners()
		self._isPartnersInit = true
	end
end


function PartnersData:updatePartners(partners)
	self:_initPartners()
	for k , v in pairs(partners) do
		local p = self._partners[v.ID]
		if p then
			p:read(v)
			p:setIsHave(true)
			p:cleanTempData()
		end
	end
	--判断当前随从是否有可激活或者可升级的
dump(self:hasPartnerCanLvOrCanActive())
	UD:updateWelfareNewPoint(Constant.FUNC_TYPE_MERCENARY, self:hasPartnerCanLvOrCanActive())
end


function PartnersData:hasPartnerCanLvOrCanActive()
	for k , v in pairs(self._partners) do
		if v and (v:canActive() or v:canLvQuality()) then
			return true
		end
	end
	return false
end

function PartnersData:getPartnerById(id)
	self:_initPartners()
	return self._partners[id]
end

function PartnersData:getPartners()
	self:_initPartners()
	return self._partners
end

function PartnersData:updateByPlayer(oldPartners, newPartners)
	local newMap = {}
	for k, v in pairs(newPartners) do
		newMap[v.id] = true
	end

	local isChange = false
	for k, v in pairs(oldPartners or {}) do
		if not newMap[k] then
			isChange = true
			local p = self:getPartnerById(v.id)
			p:setEquiped(false)   
        end
	end
	
	for k, v in pairs(newPartners) do
		local p = self:getPartnerById(v.id)
		if not p:isEquiped() then
			p:setEquiped(true)    
			isChange = true
		end
	end
	self._fightTeam = newPartners

	if isChange then
		EM:notify(ViewEvent.Partner_FightTeamChange)
	end
end

function PartnersData:updateByOper(opt, partner)
	self:_initPartners()
	local p
	if opt=="insert" or opt=="update" then
		p = self:getPartnerById(partner.ID)
		if p then
			if not p:isHave() then
				p:setIsHave(true)
				p:setNew(true)
			end
			p:read(partner)
		end
	elseif opt=="delete" then
		p = self:getPartnerById(partner.ID)
		if p then
			p:setIsHave(false)
		end
	end

	--判断当前随从是否有可激活或者可升级的
	UD:updateWelfareNewPoint(Constant.FUNC_TYPE_MERCENARY, self:hasPartnerCanLvOrCanActive())
	EM:notify(ViewEvent.Partner_DataChange, p, opt)
end


function PartnersData:updateBySpRewardPartnerInfo(partners)
	self:_initPartners()
	for k, v in pairs(partners) do
		local p = self._partners[v.ID]
		if p then
			p:read(v)
			p:setIsHave(true)
			p:cleanTempData()
		end
	end
	EM:notify(ViewEvent.Partner_SpRewardDataChange)
end


-- 清除金腰带
function PartnersData:cleanSpReward()
	self:_initPartners()
	for k, v in pairs(self._partners) do
		if v:isSpReward() then
			v:setDuration(0)
			v:setIsHave(false)
		end
	end
end
-- 更新金腰带随从
function PartnersData:updateBySpReward(data)
	local p = self:getPartnerById(data.id)
	if p then
		p:setDuration(data.duration)
		p:setIsHave(true)
		EM:notify(ViewEvent.Partner_DataChange, p)
	end
end


function PartnersData:cleanAllPartnerNew()
	for k, v in pairs(self._partners) do
		v:setNew(false)
	end
end

function PartnersData:getFightTeamPartners()
	local ps = {}
	for k, v in pairs(self._fightTeam) do
		table.insert(ps, self:getPartnerById(v.id))
	end

	return ps
end


function PartnersData:isPartnerHaveById(id)
	local p = self:getPartnerById(id)
	if p then
		return p:isHave()
	end
	return false
end


function PartnersData:getAlreadyHavePartners(isNeedSort)
	local ps = {}
	for k, v in pairs(self._partners) do
		if v:isHave() then
			table.insert(ps, v)
		end
	end
	if isNeedSort then
		table.sort(ps, PartnersData._partnerSortFunc)
	end
	return ps
end

function PartnersData._partnerSortFunc(a,b)
	local aHave = a:isHave()
	local bHave = b:isHave()

	if aHave ~= bHave then
		return aHave
	end

	local aCanActive = a:canActive()
	local bCanActive = b:canActive()

	if aCanActive ~= bCanActive then
		return aCanActive
	end


	local aEquip = a:isEquiped()
	local bEquip = b:isEquiped()

	if aEquip ~= bEquip then
		return aEquip
	end

	local aISp = a:isSpReward()
	local bIsp = b:isSpReward()

	if aISp ~= bIsp then
		return aISp
	end

	local aQ = a:getCombatPower()
	local bQ = b:getCombatPower()

	if aQ ~= bQ then
		return aQ > bQ
	end

	local astar = a:getStar()
	local bstar = b:getStar()

	if astar ~= bstar then
		return astar > bstar
	end

	return a:getSort() < b:getSort()
end
---
function PartnersData:getPartnersAndSort()
    local ps = {}
    local partners = self:getPartners()
	for k, v in pairs(partners) do
		if v:isShow() then
			table.insert(ps, v)
			v:getQuality()
		end
	end
	table.sort(ps, PartnersData._partnerSortFunc)
    return ps
end


function PartnersData:getAlreadyHavePartnersCount()
	local count = 0
	for k, v in pairs(self._partners) do
		if v:isHave() then
			count = count + 1
		end
	end
	return count
end
---
function PartnersData:C2S_EmployPartner(id)
	app.session:request("pb_partner.C2S_EmployPartner",{id = tonumber(id)},function(ret, err)
	end, nil, function(ret)
		EM:notify(ViewEvent.Partner_EmployPartnerFail)
	end)
end


function PartnersData:C2S_ChangePartner(oldId, newId)
	app.session:request("pb_partner.C2S_ChangePartner",{OldPartner = oldId, Partner = newId},function(msg)
	
	end)
end

function PartnersData:C2S_LvUpPartner(uid,itemid,num)
	app.session:request("pb_partner.C2S_LvUpPartner",{Id = tonumber(uid),ItemID = tonumber(itemid),ItemNum  = tonumber(num)},function(msg)
	
	end)
end

function PartnersData:C2S_EnhancePartnerEquipment(uid)
	app.session:request("pb_partner.C2S_EnhancePartnerEquipment",{EquipID = uid},function(ret)
		EM:notify(ViewEvent.Partner_EquipStrengthResult, ret.Ok)
	end, nil, function(msg)
		-- EM:notify(ViewEvent.Partner_EquipStrengthResult, false)
	end)
end


function PartnersData:C2S_EnhancePartnerEquipmentBatch(uid)
	app.session:request("pb_partner.C2S_EnhancePartnerEquipmentBatch",{EquipID = uid},function(ret)
		local success = rawget(ret, "success") or 0 
		local failed = rawget(ret, "failed") or 0
		EM:notify(ViewEvent.Partner_EquipStrengthTenResult, success, failed)
	end, nil, function(msg)
		-- EM:notify(ViewEvent.Partner_EquipStrengthResult, false)
	end)
end


function PartnersData:C2S_EnhancePartnerSkill(uid)
	app.session:request("pb_partner.C2S_EnhancePartnerSkill",{SkillID = uid},function(ret)
		EM:notify(ViewEvent.Partner_SkillLevelUpResult, ret.Ok)
	end, nil, function(msg)
		-- EM:notify(ViewEvent.Partner_SkillLevelUpResult, false)
	end)
end

function PartnersData:C2S_QueryPartnerProp(id)
	app.session:request("pb_partner.C2S_QueryPartnerProp", {ID = id}, function(ret)
		if ret.prop then
			EM:notify(ViewEvent.Partner_PartnerPropQuery, ret.prop)
		end
	end)
end

function PartnersData:C2S_QuerySuperTitlePartner(id)
	app.session:request("pb_partner.C2S_QuerySuperTitlePartner", {ID = id}, function(ret)
		if ret.infos then
			self:updateBySpRewardPartnerInfo(ret.infos)
		end
	end)
end


return PartnersData